#ifndef PHYSICS_H
#define PHYSICS_H

#include <vector>
#include <bullet/btBulletCollisionCommon.h>
#include <bullet/btBulletDynamicsCommon.h>
#include <bullet/LinearMath/btAlignedObjectArray.h>
#include "libphysicsexport.h"

class Level;
namespace Lib3D{class Graphics;}
namespace LibWiimote{class Controller;}
class Obstacle;

namespace LibPhysics{

	class GLDebugDrawer : public btIDebugDraw
	{
		int m_debugMode;
		Lib3D::Graphics * m_graphics;

	public:
		GLDebugDrawer();
		void setGraphics(Lib3D::Graphics * graphics);
		virtual void   drawLine(const btVector3& from,const btVector3& to,const btVector3& color);
		virtual void   drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color);
		virtual void   reportErrorWarning(const char* warningString);
		virtual void   draw3dText(const btVector3& location,const char* textString);
		virtual void   setDebugMode(int debugMode);
		virtual int    getDebugMode() const { 
			return m_debugMode;
		}
	};

	class LIBPHYSICS_API Physics{
	public:
		Physics(Level & level, LibWiimote::Controller & controller);
		~Physics();
		void update(float time, float delta_time);
		void Draw(Lib3D::Graphics & graphics);

		void TickCallback(float timeStep);	
		void EchecLol(Obstacle * obstacle);

		float   getBoneYScale(int bone);
		float *	getBoneTransform(int bone);
	private:

		enum
		{
			BODYPART_TRONC,
			BODYPART_LEFT_LEG,
			BODYPART_RIGHT_LEG,
			BODYPART_COUNT
		};

		enum
		{
			JOINT_PELVIS_LEFT_FOOT,
			JOINT_PELVIS_RIGHT_FOOT,

			JOINT_COUNT
		};



		btAlignedObjectArray<btCollisionShape*>	m_collisionShapes;
		btBroadphaseInterface*	m_broadphase;
		btCollisionDispatcher*	m_dispatcher;
		btConstraintSolver*	m_solver;
		btDefaultCollisionConfiguration* m_collisionConfiguration;
		btDiscreteDynamicsWorld*		m_dynamicsWorld;
		btTypedConstraint*		m_pickConstraint;
		btCollisionShape*	m_shootBoxShape;
		std::vector<btRigidBody*> m_obstacles;

		Level & m_level;
		LibWiimote::Controller & m_controller;
		GLDebugDrawer debugdrawer;
		bool go;

		btCollisionShape* m_shapes[BODYPART_COUNT];
		btRigidBody* m_bodies[BODYPART_COUNT];
		btTypedConstraint* m_joints[JOINT_COUNT];

		
		btRigidBody* localCreateRigidBody (btScalar mass, const btTransform& startTransform, btCollisionShape* shape);

	};

}



#endif